import db from "../data/item"
let love
export default class Item {
  constructor(_love, type) {
    love = love || _love
    this.x = 0
    this.y = 0
    this.dx = 0.5
    this.xspeed = love.math.random(0.5, 0.95)
    this.scale = 8
    this.type = type
    this.texture = love.loader.data.sheet.extend()
    this.texture.setQuad(...db[type].quad)
    this.texture.setAnchorCenter()
    this.w = this.scale*db[type].quad[2]
    this.h = this.scale*db[type].quad[3]
    this.floor = db[type].floor
  }
  setToRoad(road,lap, pos) {
    this.road = road
    this.lap = lap
    this.pos = pos
    this.y = this.getLapY(this.lap)
  }

  update(dt) {
    this.dx = this.road.playerCar.speed * this.xspeed
    this.pos = this.pos + this.dx * dt
    this.x = this.road.playerCar.pos + (this.pos - this.road.playerCar.tour)
    if (this.x > 5000 || this.x < - 2000) {
      this.destroyed = true
    }
  }
  getLapY(lap) {
    return this.road.getLapPos.bind(this.road)(lap)
  }
  draw() {
    //console.log(this.texture, this.x, this.y, 0, this.scale, this.scale)
    love.graphics.setColor("#FFFFFF")
    love.graphics.draw(this.texture, this.x, this.y, 0, this.scale, this.scale)
  }
  checkColl(self, that) {
    let dx = that.x - self.x
    let dy = that.y - self.y
    if (Math.abs(dx) < self.w / 2 && Math.abs(dy) < self.h / 2) {
      return {
        dx: dx, dy: dy,
        cx: (self.x + that.x) / 2, cy: (self.y + that.y) / 2
      }
    }
  }
  effect(){
    if (this.used){return}
    this.used = true
    let car = this.road.playerCar
    switch(this.type){
      case 0 :
        car.addHP(20)
        break
      case 1 :
        car.addBuff("power",5)
        break
      case 2 :
        car.addN2O(car.n2oMax/2)
        break
      case 3 :
        car.addBuff("unbreakable",10)
        break
      case 4 :
        car.addBuff("unstopable",10)
        break
      case 5 :
        car.addBuff("fireable",5)
        break
      case 6 :
        car.speed *= 0.7
        car.crash()
        break
      case 7 :
        car.speed *= 0.3 
        break
      case 8 :
        car.speed *=0.1
        car.crash()
        break
      case 9 :
        //加分 todo
        break
      case 10 :
        car.speed *= 1.4
    }
  }
}